﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tesis2.Models;

namespace Tesis2.Controllers
{
    public class VehiculoController : Controller
    {
        private TesisDBContext db = new TesisDBContext();

        //
        // GET: /Vehiculo/

        public ActionResult Index()
        {
            var vehiculos = db.Vehiculos.Include(v => v.MarcaVehiculo).Include(v => v.ModeloVehiculo).Include(v => v.EstadoVehiculo).Include(v => v.InstitucionVehiculo);
            return View(vehiculos.ToList());
        }

        //
        // GET: /Vehiculo/Details/5

        public ActionResult Details(int id = 0)
        {
            Vehiculo vehiculo = db.Vehiculos.Find(id);
            if (vehiculo == null)
            {
                return HttpNotFound();
            }
            return View(vehiculo);
        }

        //
        // GET: /Vehiculo/Create

        public ActionResult Create()
        {
            ViewBag.MarcaVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre");
            ViewBag.ModeloVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre");
            ViewBag.EstadoVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre");
            ViewBag.InstitucionVehiculoID = new SelectList(db.Instituciones, "InstitucionID", "Nombre");
            return View();
        }

        //
        // POST: /Vehiculo/Create

        [HttpPost]
        public ActionResult Create(Vehiculo vehiculo)
        {
            if (ModelState.IsValid)
            {
                db.Vehiculos.Add(vehiculo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.MarcaVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.MarcaVehiculoID);
            ViewBag.ModeloVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.ModeloVehiculoID);
            ViewBag.EstadoVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.EstadoVehiculoID);
            ViewBag.InstitucionVehiculoID = new SelectList(db.Instituciones, "InstitucionID", "Nombre", vehiculo.InstitucionVehiculoID);
            return View(vehiculo);
        }

        //
        // GET: /Vehiculo/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Vehiculo vehiculo = db.Vehiculos.Find(id);
            if (vehiculo == null)
            {
                return HttpNotFound();
            }
            ViewBag.MarcaVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.MarcaVehiculoID);
            ViewBag.ModeloVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.ModeloVehiculoID);
            ViewBag.EstadoVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.EstadoVehiculoID);
            ViewBag.InstitucionVehiculoID = new SelectList(db.Instituciones, "InstitucionID", "Nombre", vehiculo.InstitucionVehiculoID);
            return View(vehiculo);
        }

        //
        // POST: /Vehiculo/Edit/5

        [HttpPost]
        public ActionResult Edit(Vehiculo vehiculo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(vehiculo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MarcaVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.MarcaVehiculoID);
            ViewBag.ModeloVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.ModeloVehiculoID);
            ViewBag.EstadoVehiculoID = new SelectList(db.Parametros, "ParametroID", "Nombre", vehiculo.EstadoVehiculoID);
            ViewBag.InstitucionVehiculoID = new SelectList(db.Instituciones, "InstitucionID", "Nombre", vehiculo.InstitucionVehiculoID);
            return View(vehiculo);
        }

        //
        // GET: /Vehiculo/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Vehiculo vehiculo = db.Vehiculos.Find(id);
            if (vehiculo == null)
            {
                return HttpNotFound();
            }
            return View(vehiculo);
        }

        //
        // POST: /Vehiculo/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Vehiculo vehiculo = db.Vehiculos.Find(id);
            db.Vehiculos.Remove(vehiculo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (db != null)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
        }
    }
}